import Vue from 'vue';
import VueRouter from 'vue-router';

import Index from '../components/Index.vue';
import Posts from '../components/user/Posts.vue';
import UserProfile from '../components/user/Profile.vue';
import Login from '../components/login/Login.vue';

//route 分支
import Setting from './setting.js';
import Product from './productRoute';
import FinanceManage from './financeManage';

Vue.use(VueRouter);


const routes = [
  { path: '/', component: Index,
    children: [
      { path: 'profile', component: UserProfile },
      { path: 'posts', component: Posts },
      ...Setting,
      ...Product,
      ...FinanceManage,
    ]
  },
  { path: '/login', component: Login, name: "login" }
];


const router =  new VueRouter({
  // mode: 'history',
  routes,
});


router.beforeEach((to, from , next) => {
  let path = to.path;
  if (path === '/login') {
    next();
  }
  if (localStorage.getItem('__session__')) {
    // store.dispatch('changeCurrPath', to.path)
    next();
  } else {
    router.push({ path: '/login' });
    next();
  }

});

export default router;